package cn.blankchn.www.util;

import cn.blankchn.www.entity.Part;
import cn.blankchn.www.entity.Time;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by apple on 2018/4/2.
 */
public class ReadCsvFactory {

    public static ArrayList<Part> handle(String path, int fileNum) {

        StringBuilder sb = new StringBuilder();
        ArrayList<Part> list_part = new ArrayList<Part>();

        try {

            File file = new File(path);


            if (file.isFile() && file.exists()) {
                //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file));//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                Part part = new Part();
                part.setNum(fileNum);
                boolean while_zero = true;
                int num = 0;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    String[] strs = lineTxt.split(",");
                    if (strs[0].trim().length() <= 0 || strs[1].trim().length() <= 0) {
                        throw new Exception(file.getName() + "文件中在第" + num + "行中数据处理错误，不存在speed或者accelation");
                    }

                    double speed = Double.parseDouble(strs[0]);
                    double acce = Double.parseDouble(strs[1]);
                    Time time = new Time(num, speed, acce);
                    num++;
                    if (speed != 0) {
                        if (while_zero) {
                            while_zero = false;
                        }
                        part.getList().add(time);
                        if (acce > 0) {
                            part.getList_up().add(time);
                        } else if (acce < 0) {
                            part.getList_down().add(time);
                        }else if(acce==0){
                            part.getList_avg().add(time);
                        }
                        continue;
                    }

                    if (speed == 0) {
                        if (while_zero) {
                            part.getList().add(time);
                            if(acce==0){
                                part.getList_zero().add(time);
                            }else if (acce > 0) {
                                part.getList_up().add(time);
                            } else if (acce < 0) {
                                part.getList_down().add(time);
                            }
                            continue;
                        } else {
                            list_part.add(part);
                            part = new Part();
                            part.setNum(fileNum);
                            part.getList().add(time);
                            if(acce==0){
                                part.getList_zero().add(time);
                            }else if (acce > 0) {
                                part.getList_up().add(time);
                            } else if (acce < 0) {
                                part.getList_down().add(time);
                            }
                            while_zero = true;
                        }
                    }


                }

                list_part.add(part);


                read.close();
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return list_part;
    }


}
